package javango.forms.widgets;

import java.util.Map;

public abstract class Input extends WidgetBase {

	
	public Input() {
		super(null);
	}
	
	public Input(Map<String, Object> attrs) {
		super(attrs);
	}

	public String render(String name, Object value, Map<String, Object> attrs) {		
		Map<String, Object> finalAttrs = buildAttrs(attrs);
		finalAttrs.put("type", getInputType());
		finalAttrs.put("name", name);
		
		if (finalAttrs.containsKey("id")) {
			finalAttrs.put("id", String.format((String)finalAttrs.get("id"), name));
		}
		
		if (value != null) {
			if (value instanceof String) {
				if (!"".equals(value)) {
					finalAttrs.put("value", value);
				}				
			} else if (value instanceof String[]){
				String[] values = (String[])value;
				if (values.length > 0 && !"".equals(values[0])) {
					finalAttrs.put("value", values[0]);
				}	
			} else {
				finalAttrs.put("value", value.toString());
			}
		}
				
		return String.format("<input%s />", Util.flatatt(finalAttrs));
	}

	public abstract String getInputType();
}
